<?php
/* $Id$ */
/*
	firewall_mac.php
	originally part of stairway
    Copyright (C) 20095 Wang Zhongliang (wzlsh629@163.com)
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

##|+PRIV
##|*IDENT=page-firewall-mac
##|*NAME=Firewall: Mac page
##|*DESCR=Allow access to the 'Firewall: Mac' page.
##|*MATCH=firewall_mac.php*
##|-PRIV


$pgtitle = array("防火墙", "MAC过滤");
$pgmenu = array('firewall', '/firewall_mac.php');
require("guiconfig.inc");

if (!is_array($config['filter']['macrule'])) {
	$config['filter']['macrule'] = array();
}

$a_filter = &$config['filter']['macrule'];

if ($_POST) {

	$pconfig = $_POST;

	if ($_POST['apply']) {
		$retval = 0;
		config_lock();
		$retval = mac_configure();
		config_unlock();

		if (file_exists($d_macconfdirty_path))
			unlink($d_macconfdirty_path);

		$savemsg = "设置已被应用，防火墙规则正在后台重新载入。您可以<a href='status_filter_reload.php'>查看</a>重载进度。";
	}
}

if ($_GET['macdefault'] == "pass" || $_GET['macdefault'] == "block") {
                $config['filter']['macdefault'] = $_GET['macdefault'];
                write_config();
                touch($d_macconfdirty_path);
                header("Location: firewall_mac.php");
                exit;
        }

if ($_GET['act'] == "del") {
        if ($a_filter[$_GET['id']]) {
                unset($a_filter[$_GET['id']]);
                write_config();
                touch($d_macconfdirty_path);
                header("Location: firewall_mac.php");
                exit;
        }
}
if (isset($_POST['del_x'])) {
    /* delete selected rules */
    if (is_array($_POST['rule']) && count($_POST['rule'])) {
        foreach ($_POST['rule'] as $rulei) {
            unset($a_filter[$rulei]);
        }
        write_config();
        touch($d_macconfdirty_path);
        header("Location: firewall_mac.php");
        exit;
    }
}else if ($_GET['act'] == "toggle") {
	if ($a_filter[$_GET['id']]) {
                if(isset($a_filter[$_GET['id']]['disabled']))
                        unset($a_filter[$_GET['id']]['disabled']);
                else
                        $a_filter[$_GET['id']]['disabled'] = true;
		write_config();
		touch($d_macconfdirty_path);
		header("Location: firewall_mac.php");
		exit;
	}        
}
        
$closehead = false;

include("head.inc");

echo "<script type=\"text/javascript\" language=\"javascript\" src=\"/javascript/domTT/domLib.js\"></script>";
echo "<script type=\"text/javascript\" language=\"javascript\" src=\"/javascript/domTT/domTT.js\"></script>";
echo "<script type=\"text/javascript\" language=\"javascript\" src=\"/javascript/domTT/behaviour.js\"></script>";
echo "<script type=\"text/javascript\" language=\"javascript\" src=\"/javascript/domTT/fadomatic.js\"></script>";
?>
</head>
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<form action="firewall_mac.php" method="post">
<?php if ($savemsg) print_info_box($savemsg); ?>
<?php if (file_exists($d_macconfdirty_path)): ?><p>
<?php print_info_box_np("MAC过滤设置已更改。<br>您需要应用更改使其生效。");?><br>
<?php endif; ?>
</form>
<form action="firewall_mac.php" method="post">
<script type="text/javascript" language="javascript" src="/javascript/row_toggle.js">
</script>
<div class="soft_cont_right_table">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
    <td>
	<div id="mainarea">
             <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr id="frheader">
                  <td width="3%" class="list">&nbsp;</td>
                  <td width="5%" class="list">&nbsp;</td>
                  <td width="42%" class="listhdrr">MAC地址</td>
                  <td width="40%" class="listhdr">描述</td>
                  <td width="10%" class="list">
			<table border="0" cellspacing="0" cellpadding="1">
			   <tr>
				<?php
					$nrules = 0;
					for ($i = 0; isset($a_filter[$i]); $i++) {
						$filterent = $a_filter[$i];
						$nrules++;
					}
				?>
				<td>
				<?php if ($nrules == 0): ?>
				<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x_d.gif" width="17" height="17" title="删除所选规则" border="0"><?php else: ?>
				<input name="del" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" title="删除所选规则" onclick="return confirm('是否删除所选规则')"><?php endif; ?>
				</td>
				<td align="center" valign="middle"><a href="firewall_mac_edit.php"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="添加" width="17" height="17" border="0"></a></td>
			   </tr>
			</table>
		  </td>
		</tr>
				<?php $nrules = 0; for ($i = 0; isset($a_filter[$i]); $i++):
					$filterent = $a_filter[$i];
				?>
                <tr valign="top" id="fr<?=$nrules;?>">
                  <td class="listt"><input type="checkbox" id="frc<?=$nrules;?>" name="rule[]" value="<?=$i;?>" onClick="fr_bgcolor('<?=$nrules;?>')" style="margin: 0; padding: 0; width: 15px; height: 15px;"></td>
                  <td class="listt" align="center">
				  <?php if ($filterent['type'] == "block")
				  			$iconfn = "block";
						else
							$iconfn = "pass";
						if (isset($filterent['disabled'])) {
							$textss = "<span class=\"gray\">";
							$textse = "</span>";
							$iconfn .= "_d";
						} else {
							$textss = $textse = "";
						}
				  ?>
				  <a href="?if=<?=$if;?>&act=toggle&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_<?=$iconfn;?>.gif" width="11" height="11" border="0" title=""></a>
				  <?php if (isset($filterent['log'])):
							$iconfnlog = "log_s";
						if (isset($filterent['disabled']))
							$iconfnlog .= "_d";
				  	?>
				  <br><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_<?=$iconfnlog;?>.gif" width="11" height="15" border="0">
				  <?php endif; ?>
				  </td>
                  <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_mac_edit.php?id=<?=$i;?>';">
                    <?=$textss;?><?php if (isset($filterent['address'])) echo strtoupper($filterent['address']);?><?=$textse;?>
                  </td>
                  <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='firewall_mac_edit.php?id=<?=$i;?>';" class="descr">
                    <?=$textss;?><?=htmlspecialchars($filterent['descr']);?>&nbsp;<?=$textse;?>
                  </td>
                  <td valign="middle" nowrap class="list">
				    <table border="0" cellspacing="0" cellpadding="1">
					<tr>
					  <td><a href="firewall_mac_edit.php?id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" title="编辑" width="17" height="17" border="0"></a></td>
					</tr>
					<tr>
					  <td align="center" valign="middle"><a href="firewall_mac.php?act=del&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0" title="删除" onclick="return confirm('是否删除该规则？')"></a></td>
					</tr>
					</table>
				  </td>
				</tr>
			  <?php $nrules++; endfor; ?>
			  <?php if ($nrules == 0): ?>
              <td class="listt"></td>
			  <td class="listt"></td>
			  <td class="listlr" colspan="9" align="center" valign="middle">
			  <span class="gray">
			  当前没有定义任何规则。
			  <br><br>
			  点击 <a href="firewall_mac_edit.php?if=<?=$if;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="添加" border="0" width="17" height="17" align="absmiddle"></a> 按钮新增一条规则。</span>
			  </td>
			  <?php endif; ?>
                <tr>
                  <td width="3%" class="list">&nbsp;</td>
                  <td width="5%" class="list">&nbsp;</td>
                  <td width="42%" class="list"></td>
                  <td width="40%" class="list"></td>
                  <td width="10%" class="list">
            <table border="0" cellspacing="0" cellpadding="1">
               <tr>
                <td>
                <?php if ($nrules == 0): ?>
                <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x_d.gif" width="17" height="17" title="删除所选规则" border="0"><?php else: ?>
                <input name="del" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" title="删除所选规则" onclick="return confirm('是否删除所选规则？')"><?php endif; ?>
                </td>
                <td align="center" valign="middle"><a href="firewall_mac_edit.php"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="添加" width="17" height="17" border="0"></a></td>
               </tr>
            </table>
          </td>
        </tr>			  
              </table>
	      <table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_pass.gif" width="11" height="11"></td>
                  <td>通过</td>
                  <td width="14"></td>
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_block.gif" width="11" height="11"></td>
                  <td>阻止</td>
                </tr>
                <tr>
                  <td><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_pass_d.gif" width="11" height="11"></td>
                  <td nowrap>通过 (无效)</td>
                  <td>&nbsp;</td>
                  <td><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_block_d.gif" width="11" height="11"></td>
                  <td nowrap>阻止 (无效)</td>
                </tr>
		<tr>
		  <td colspan="10">
  <p>
  <strong><span class="red">提示：<br>
  </span></strong>MAC地址可以用来阻止局域网内的指定机器对外网的访问。MAC地址的过滤优先于规则管理中的规则。</p>
		 </td>
	        </tr>
              </table>
	</div>
    </td>
  </tr>
</table>
</div>
  <input type="hidden" name="if" value="<?=$if;?>">
</form>
<?php include("fend.inc"); ?>
</body>
</html>
